package com.xingji.caixia.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.xingji.caixia.R;
import com.xingji.caixia.Constant;
import com.xingji.caixia.entitys.SchoolData;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.ArrayList;
import java.util.List;
import pl.coreorb.selectiondialogs.data.SelectableColor;
import pl.coreorb.selectiondialogs.data.SelectableIcon;
import pl.coreorb.selectiondialogs.dialogs.ColorSelectDialog;

public class LoginActivity extends BaseActivity implements View.OnClickListener,ColorSelectDialog.OnColorSelectedListener {

    SharedPreferences shared ;
    EditText groupname;
    EditText username;
    EditText password;
    Button login;
    private List<SchoolData> schoolist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);

        shared = getSharedPreferences("user", MODE_PRIVATE);
        if (shared.getBoolean("islogin",false)) {
            String school = shared.getString("school_id", "");
            startActivity(new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("school_id", school));
            finish();
            return;
        }

        groupname = (EditText) findViewById(R.id.groupname);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                String schools = groupname.getText().toString().trim();
                String usernames = username.getText().toString().trim();
                String passwords = password.getText().toString().trim();
                if (TextUtils.isEmpty(schools)) {
                    Toast.makeText(this, "请输入机构编码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(usernames)) {
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(passwords)) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                Login(schools, usernames, passwords);
                break;
        }
    }



    public void Login(String school, String username, String password) {

        final ProgressDialog progressDialog = new ProgressDialog(this);

        RequestParams requestParams = new RequestParams(Constant.LOGIN);
        requestParams.addBodyParameter("organization_code", school);
        requestParams.addBodyParameter("username", username);
        requestParams.addBodyParameter("password", password);
        post(requestParams,1);
    }

    @Override
    public void postSuccess(JSONObject jsonObject, Integer tag) {
        super.postSuccess(jsonObject, tag);
        try {
            if (jsonObject.getBoolean("status")){
                schoolist = JSON.parseArray(jsonObject.getString("list"),SchoolData.class);
                ArrayList<SelectableIcon> selectionDialogsColors = new ArrayList<>();
                ArrayList<SelectableColor> selectableColors = new ArrayList<SelectableColor>();
                for (int i=0;i<schoolist.size();i++){
                    SchoolData data = schoolist.get(i);
                    //从开源库中随便抽一些颜色来用用...学校图片放上去不合适
                    SelectableColor color = new SelectableColor(
                            data.getSchool_id(),
                            data.getSchool_name(),
                            LoginActivity.this.getResources().getIntArray(pl.coreorb.selectiondialogs.R.array.selectiondialogs_materialdesign500_colors)[i]);
                    selectableColors.add(color);
                }

                new ColorSelectDialog.Builder(LoginActivity.this)
                        .setColors(selectableColors)
                        .setTitle("请选择校区")
                        .setOnColorSelectedListener(LoginActivity.this)
                        .build()
                        .show(LoginActivity.this.getSupportFragmentManager(),"login");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onColorSelected(SelectableColor selectedItem) {
        SharedPreferences.Editor editor = shared.edit();
        editor.putBoolean("islogin",true);
        editor.putString("school_id",selectedItem.getId());
        editor.apply();
        editor.commit();
        Log.e("item",selectedItem.toString());
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("school_id", selectedItem.getId());
        startActivity(intent);
        this.finish();
    }
}
